package com.project.school_flate.serviceimpl.flow;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.project.school_flate.dto.flow.FlowMoneyDto;
import com.project.school_flate.entity.flow.FlowMoney;
import com.project.school_flate.mapper.flow.FlowBusinessMapper;
import com.project.school_flate.mapper.flow.FlowMoneyMapper;
import com.project.school_flate.service.flow.FlowMoneyService;
import com.project.school_flate.util.PoToDTO;
import com.project.school_flate.util.Result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service(value = "flowMoneyServiceImpl")
public class FlowMoneyServiceImpl implements FlowMoneyService {

    @Autowired
    private FlowMoneyMapper oFlowMoneyMapper;

    @Autowired
    private FlowBusinessMapper oFlowBusinessMapper;

    /**
     * 获取平台流水
     * @param oFlowMoneyDto
     * @return
     * @throws Exception
     */
    @Override
    public Result getFlowMoney(FlowMoneyDto oFlowMoneyDto) throws Exception {
        List<FlowMoney> oFlowMoneyList = new ArrayList<>();
        long total = 0;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderBy("create_time desc");
        //是否分页
        if(oFlowMoneyDto.getPage() != null && oFlowMoneyDto.getLimit() != null){
            Page<FlowMoney> FlowMoneyPage = oFlowMoneyMapper.paginateWithRelations(oFlowMoneyDto.getPage(),oFlowMoneyDto.getLimit(),queryWrapper);
            oFlowMoneyList = FlowMoneyPage.getRecords();
            total = FlowMoneyPage.getTotalRow();
        }else{
            oFlowMoneyList = oFlowMoneyMapper.selectListWithRelationsByQuery(queryWrapper);
            total = oFlowMoneyList.size();
        }
        //PoToDto
        List<FlowMoneyDto> oFlowMoneyDtoList = (List<FlowMoneyDto>) PoToDTO.poToDtoList(oFlowMoneyList,new FlowMoneyDto());
        return Result.ok(oFlowMoneyDtoList,total);
    }

}
